﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace FTPLib
{
    public class ftpHelper
    {
        #region 属性

        /// <summary>
        /// FTP服务器IP地址
        /// </summary>
        private string strRemoteHost;
        public string RemoteHost
        {
            get
            {
                return strRemoteHost;
            }
            set
            {
                strRemoteHost = value;
            }
        }

        /// <summary>
        /// FTP服务器端口
        /// </summary>
        private int strRemotePort;
        public int RemotePort
        {
            get
            {
                return strRemotePort;
            }
            set
            {
                strRemotePort = value;
            }
        }

        /// <summary>
        /// 当前服务器目录
        /// </summary>
        private string strRemotePath;
        public string RemotePath
        {
            get
            {
                return strRemotePath;
            }
            set
            {
                strRemotePath = value;
            }
        }

        /// <summary>
        /// 登录用户账号
        /// </summary>
        private string strRemoteUser;
        public string RemoteUser
        {
            set
            {
                strRemoteUser = value;
            }
        }

        /// <summary>
        /// 用户登录密码
        /// </summary>
        private string strRemotePass;
        public string RemotePass
        {
            set
            {
                strRemotePass = value;
            }
        }

        /// <summary>
        /// 是否登录
        /// </summary>
        private Boolean bConnected;
        public bool Connected
        {
            get
            {
                return bConnected;
            }
        }

        /// <summary>
        /// 状态信息
        /// </summary>
        private string strMessage;
        public string Message
        {
            get
            {
                return strMessage.Trim();
            }
        }

        #endregion

        /// <summary>
        /// 缺省构造函数
        /// </summary>
        public ftpHelper()
        {
            strRemoteHost = "";
            strRemotePath = "";
            strRemoteUser = "";
            strRemotePass = "";
            strRemotePort = 21;
            bConnected = false;
            strMessage = "";
        }

        public bool Connect()
        {
            IPEndPoint ep;
            try
            {
                socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
            }
            catch (Exception ex)
            {
                strMessage = ex.Message;
                return false;
            }
            // 链接
            try
            {
                socketControl.Connect(ep);
            }
            catch (Exception)
            {
                strMessage = "服务器连接失败";
                return false;
            }

            // 获取应答码
            ReadReply();
            if (iReplyCode != 220)
            {
                DisConnect();
                strMessage = strReply.Substring(4);
                return false;
            }
        }
    }
}
